from django.db import models
from django.contrib.auth.models import AbstractUser

import uuid
from datetime import datetime

# Create your models here.


def upload_path(instance,filename):
    after_dot = filename.split('.')[-1]
    filename = f'{uuid.uuid4()}.{after_dot}'
    now = datetime.now()
    return f'header/{now.year}{now.month}{now.day}/{filename}'







class User(AbstractUser):

    phone = models.CharField(max_length=11,unique=True)

    class Meta:
        db_table = 'user'



class Goods(models.Model):
    name = models.CharField(max_length=100)
    sale = models.IntegerField()
    price = models.IntegerField()
    image = models.ImageField(upload_to=upload_path,null=True,blank=True)
    info = models.CharField(max_length=100)
    created_time = models.DateTimeField(auto_now_add=True)
    updated_time = models.DateTimeField(auto_now_add=True)


    class Meta:
        db_table = 'goods'



class Cart(models.Model):

    goods = models.ForeignKey(Goods,on_delete=models.CASCADE,related_name='goods')
    user = models.ForeignKey(User,on_delete=models.CASCADE,related_name='user')
    score = models.IntegerField(default=0,verbose_name='商品积分')
    weight = models.CharField(max_length=100,verbose_name='商品重量',default='0kg')
    quantity = models.IntegerField(default=1,verbose_name='购买数量')
    is_selected = models.IntegerField(default=0,verbose_name='是否选中')
    created_time = models.DateTimeField(auto_now_add=True)
    updated_time = models.DateTimeField(auto_now_add=True)


    class Meta:
        db_table = 'cart'



